home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / ui / scrollprintcontrol.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  11.7 KB  |  337 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Local
  23. from base.g import *
  24. from base import utils
  25. from prnt import cups
  26.  
  27. # Qt
  28. from qt import *
  29. from scrollview import ScrollView
  30.  
  31. # Std Lib
  32. import os.path, os
  33. import time
  34.  
  35.  
  36. class CancelJobPushButton(QPushButton):
  37.     def __init__(self, parent, name, job_id):
  38.         QPushButton.__init__(self, parent, name)
  39.         self.job_id = job_id
  40.  
  41.  
  42. class ScrollPrintJobView(ScrollView):
  43.     def __init__(self,parent = None,name = None,fl = 0):
  44.         ScrollView.__init__(self,parent,name,fl)
  45.         
  46.         #self.heading_color = "#cccccc"
  47.         
  48.         self.JOB_STATES = { cups.IPP_JOB_PENDING : self.__tr("Pending"),
  49.                             cups.IPP_JOB_HELD : self.__tr("On hold"),
  50.                             cups.IPP_JOB_PROCESSING : self.__tr("Printing"),
  51.                             cups.IPP_JOB_STOPPED : self.__tr("Stopped"),
  52.                             cups.IPP_JOB_CANCELLED : self.__tr("Canceled"),
  53.                             cups.IPP_JOB_ABORTED : self.__tr("Aborted"),
  54.                             cups.IPP_JOB_COMPLETED : self.__tr("Completed"),
  55.                            }
  56.  
  57.         self.warning_pix = QPixmap(os.path.join(prop.image_dir, "warning.png"))
  58.         self.error_pix = QPixmap(os.path.join(prop.image_dir, "error.png"))
  59.         self.ok_pix = QPixmap(os.path.join(prop.image_dir, "ok.png"))
  60.         self.busy_pix = QPixmap(os.path.join(prop.image_dir, 'busy.png'))
  61.         self.idle_pix = QPixmap(os.path.join(prop.image_dir, 'idle.png'))
  62.         self.print_pix = QPixmap(os.path.join(prop.image_dir, "print_icon.png"))
  63.  
  64.         self.JOB_STATE_ICONS = { cups.IPP_JOB_PENDING: self.busy_pix,
  65.                                  cups.IPP_JOB_HELD : self.busy_pix,
  66.                                  cups.IPP_JOB_PROCESSING : self.print_pix,
  67.                                  cups.IPP_JOB_STOPPED : self.warning_pix,
  68.                                  cups.IPP_JOB_CANCELLED : self.warning_pix,
  69.                                  cups.IPP_JOB_ABORTED : self.error_pix,
  70.                                  cups.IPP_JOB_COMPLETED : self.ok_pix,
  71.                                 }
  72.  
  73.  
  74.     def fillControls(self):
  75.         ScrollView.fillControls(self)
  76.         
  77.         self.addGroupHeading("print_control", self.__tr("Print Control"))
  78.         
  79.         self.addPrintController()
  80.         self.updatePrintController()
  81.  
  82.         jobs = cups.getJobs()
  83.  
  84.         num_jobs = 0
  85.         for j in jobs:
  86.             if j.dest == self.cur_printer: 
  87.                 num_jobs += 1
  88.  
  89.         if num_jobs > 1:
  90.             self.addGroupHeading("job_control", self.__tr("Job Control"))
  91.             self.addCancelAllJobsController()
  92.  
  93.  
  94.         if num_jobs:
  95.             if num_jobs == 1:
  96.                 self.addGroupHeading("jobs", self.__tr("1 Active Print Job"))
  97.  
  98.             elif num_jobs > 1:
  99.                 self.addGroupHeading("jobs", self.__tr("%1 Active Print Jobs").arg(num_jobs))
  100.  
  101.             for j in jobs:
  102.                 if j.dest == self.cur_printer: 
  103.                     self.addItem(j.dest, j.id, j.state, j.user, j.title)
  104.  
  105.  
  106.     def addPrintController(self):
  107.         widget = self.getWidget()
  108.  
  109.         layout1 = QGridLayout(widget,1,1,5,10,"layout1")
  110.  
  111.         layout2 = QVBoxLayout(None,0,6,"layout2")
  112.  
  113.         self.stopstartTextLabel = QLabel(widget,"stopstartTextLabel")
  114.         layout2.addWidget(self.stopstartTextLabel)
  115.  
  116.         self.acceptrejectTextLabel = QLabel(widget,"acceptrejectTextLabel")
  117.         layout2.addWidget(self.acceptrejectTextLabel)
  118.  
  119.         self.defaultTextLabel = QLabel(widget,"defaultTextLabel")
  120.         layout2.addWidget(self.defaultTextLabel)
  121.  
  122.         layout1.addMultiCellLayout(layout2,2,3,0,0)
  123.  
  124.         layout3 = QVBoxLayout(None,0,6,"layout3")
  125.  
  126.         self.stopstartPushButton = QPushButton(widget,"stopstartPushButton")
  127.         layout3.addWidget(self.stopstartPushButton)
  128.  
  129.         self.rejectacceptPushButton = QPushButton(widget,"rejectacceptPushButton")
  130.         layout3.addWidget(self.rejectacceptPushButton)
  131.  
  132.         self.defaultPushButton = QPushButton(widget,"defaultPushButton")
  133.         layout3.addWidget(self.defaultPushButton)
  134.         layout1.addMultiCellLayout(layout3,2,3,2,2)
  135.         
  136.         spacer1 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  137.         layout1.addItem(spacer1,3,1)
  138.         
  139.         spacer2 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  140.         layout1.addItem(spacer2,2,1)
  141.  
  142.         self.connect(self.stopstartPushButton,SIGNAL("clicked()"),self.stopstartPushButton_clicked)
  143.         self.connect(self.rejectacceptPushButton,SIGNAL("clicked()"),self.rejectacceptPushButton_clicked)
  144.         self.connect(self.defaultPushButton,SIGNAL("clicked()"),self.defaultPushButton_clicked)
  145.  
  146.         self.addWidget(widget, "print_control")
  147.  
  148.     def updatePrintController(self):
  149.         # default printer
  150.         self.defaultPushButton.setText(self.__tr("Set as Default"))
  151.         default_printer = cups.getDefaultPrinter()
  152.  
  153.         if default_printer == self.cur_printer:
  154.             s = self.__tr("SET AS DEFAULT")
  155.             self.defaultPushButton.setEnabled(False)
  156.         else:
  157.             s = self.__tr("NOT SET AS DEFAULT")
  158.             self.defaultPushButton.setEnabled(True)
  159.  
  160.         self.defaultTextLabel.setText(self.__tr("The printer is currently: %1").arg(s))
  161.  
  162.         cups_printers = cups.getPrinters()
  163.         for p in cups_printers:
  164.             if p.name == self.cur_printer:
  165.                 self.printer_state = p.state
  166.                 self.printer_accepting = p.accepting
  167.                 break
  168.  
  169.         # start/stop
  170.         if self.printer_state == cups.IPP_PRINTER_STATE_IDLE:
  171.             s = self.__tr("IDLE")
  172.             self.stopstartPushButton.setText(self.__tr("Stop Printer"))
  173.  
  174.         elif self.printer_state == cups.IPP_PRINTER_STATE_PROCESSING:
  175.             s = self.__tr("PROCESSING")
  176.             self.stopstartPushButton.setText(self.__tr("Stop Printer"))
  177.  
  178.         else:
  179.             s = self.__tr("STOPPED")
  180.             self.stopstartPushButton.setText(self.__tr("Start Printer"))
  181.  
  182.         self.stopstartTextLabel.setText(self.__tr("The printer is currently: %1").arg(s))
  183.  
  184.         # reject/accept
  185.         if self.printer_accepting:
  186.             s = self.__tr("ACCEPTING JOBS")
  187.             self.rejectacceptPushButton.setText(self.__tr("Reject Jobs"))
  188.         else:
  189.             s = self.__tr("REJECTING JOBS")
  190.             self.rejectacceptPushButton.setText(self.__tr("Accept Jobs"))
  191.  
  192.         self.acceptrejectTextLabel.setText(self.__tr("The printer is currently: %1").arg(s))
  193.  
  194.  
  195.     def stopstartPushButton_clicked(self):
  196.         QApplication.setOverrideCursor(QApplication.waitCursor)
  197.         try:
  198.             if self.printer_state in (cups.IPP_PRINTER_STATE_IDLE, cups.IPP_PRINTER_STATE_PROCESSING):
  199.                 cups.stop(self.cur_printer)
  200.             else:
  201.                 cups.start(self.cur_printer)
  202.  
  203.             self.updatePrintController()
  204.         finally:
  205.             QApplication.restoreOverrideCursor()
  206.  
  207.     def rejectacceptPushButton_clicked(self):
  208.         QApplication.setOverrideCursor(QApplication.waitCursor)
  209.         try:
  210.             if self.printer_accepting:
  211.                 cups.reject(self.cur_printer)
  212.             else:
  213.                 cups.accept(self.cur_printer)
  214.  
  215.             self.updatePrintController()
  216.         finally:
  217.             QApplication.restoreOverrideCursor()
  218.  
  219.  
  220.     def defaultPushButton_clicked(self):
  221.         QApplication.setOverrideCursor(QApplication.waitCursor)
  222.         try:
  223.             result = cups.setDefaultPrinter(self.cur_printer)
  224.             if not result:
  225.                 log.error("Set default printer failed.")
  226.             else:
  227.                 self.updatePrintController()
  228.         finally:
  229.             QApplication.restoreOverrideCursor()
  230.  
  231.  
  232.     def addCancelAllJobsController(self):
  233.         widget = self.getWidget()
  234.  
  235.         layout1 = QHBoxLayout(widget,10,5,"layout1")
  236.  
  237.         textLabel1 = QLabel(widget,"textLabel1")
  238.         layout1.addWidget(textLabel1)
  239.  
  240.         spacer1 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  241.         layout1.addItem(spacer1)
  242.  
  243.         cancelPushButton = QPushButton(widget,"cancelPushButton")
  244.         layout1.addWidget(cancelPushButton)
  245.  
  246.         self.addWidget(widget, "job_control")
  247.  
  248.         textLabel1.setText(self.__tr("Cancel all active print jobs"))
  249.         cancelPushButton.setText(self.__tr("Cancel All Jobs"))
  250.  
  251.         self.connect(cancelPushButton, SIGNAL("clicked()"), self.cancelAllJobs)
  252.  
  253.  
  254.     def cancelAllJobs(self):
  255.         QApplication.setOverrideCursor(QApplication.waitCursor)
  256.         try:
  257.             if not cups.purge(self.cur_printer):
  258.                 log.error("Cancel all jobs failed.")
  259.             else:
  260.                 self.fillControls()
  261.         finally:
  262.             QApplication.restoreOverrideCursor()
  263.  
  264.  
  265.     def addItem(self, dest, job_id, state, user, title):
  266.         widget = self.getWidget()
  267.  
  268.         layout1 = QGridLayout(widget,1,1,5,10,"layout1")
  269.  
  270.         #line1 = QFrame(widget,"line1")
  271.         #line1.setFrameShape(QFrame.HLine)
  272.  
  273.         #layout1.addMultiCellWidget(line1,3,3,0,4)
  274.  
  275.         cancelPushButton = CancelJobPushButton(widget,"cancelPushButton", job_id)
  276.         layout1.addWidget(cancelPushButton,1,4)
  277.  
  278.         spacer1 = QSpacerItem(30,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  279.         layout1.addItem(spacer1,1,3)
  280.  
  281.         icon = QLabel(widget,"icon")
  282.         icon.setSizePolicy(QSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed,0,0,
  283.             icon.sizePolicy().hasHeightForWidth()))
  284.  
  285.         icon.setMinimumSize(QSize(32,32))
  286.         icon.setMaximumSize(QSize(32,32))
  287.         icon.setScaledContents(1)
  288.         layout1.addMultiCellWidget(icon,1,2,1,1)
  289.  
  290.         spacer2 = QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
  291.         layout1.addItem(spacer2,0,3)
  292.  
  293.         titleText = QLabel(widget,"titleText")
  294.         titleText.setSizePolicy(QSizePolicy(QSizePolicy.Expanding,QSizePolicy.Preferred,0,0,
  295.             titleText.sizePolicy().hasHeightForWidth()))
  296.  
  297.         layout1.addMultiCellWidget(titleText,0,0,1,2)
  298.  
  299.         stateText = QLabel(widget,"stateText")
  300.         stateText.setSizePolicy(QSizePolicy(QSizePolicy.Expanding,QSizePolicy.Preferred,0,0,
  301.             stateText.sizePolicy().hasHeightForWidth()))
  302.  
  303.         layout1.addWidget(stateText,1,2)
  304.  
  305.         jobIDText = QLabel(widget,"jobIDText")
  306.         layout1.addWidget(jobIDText,0,4)
  307.  
  308.         titleText.setText(self.__tr("<b>%1</b>").arg(title))
  309.         stateText.setText(self.JOB_STATES[state])
  310.         jobIDText.setText(self.__tr("Job ID: %1").arg(job_id))
  311.         cancelPushButton.setText(self.__tr("Cancel Job"))
  312.  
  313.         icon.setPixmap(self.JOB_STATE_ICONS[state])
  314.  
  315.         self.connect(cancelPushButton, SIGNAL("clicked()"), self.cancelJob)
  316.  
  317.         self.addWidget(widget, dest+str(job_id))
  318.  
  319.  
  320.     def cancelJob(self):
  321.         sender = self.sender()
  322.  
  323.         job_id = sender.job_id
  324.  
  325.         QApplication.setOverrideCursor(QApplication.waitCursor)
  326.         try:
  327.             self.cur_device.cancelJob(job_id)
  328.             time.sleep(1)
  329.         finally:
  330.             QApplication.restoreOverrideCursor()
  331.  
  332.         self.fillControls()
  333.  
  334.     def __tr(self,s,c = None):
  335.         return qApp.translate("DevMgr4",s,c)
  336.         
  337.